<?php

require_once( "PhpFaces/Component/UIComponent.php" );
require_once( "PhpFaces/Render/Render.php" );

class PhpFaces_RenderKit_Html_HtmlBasicRenderKitImpl extends PhpFaces_Render_RenderKit {
	
	
	private $renderers;
	public function __construct() {
		$this->renderers = array();
	}
	
	public function addRenderer( $family, $rendererType, PhpFaces_Render_Renderer $renderer ) {
		$this->renderers[ $this->key( $family, $rendererType ) ] = $renderer;
	}
	
	public function getRenderer( $family, $rendererType ) {
		return $this->renderers[ $this->key( $family, $rendererType ) ];
	}
	protected function key( $family, $rendererType ) {
		return "$family.$rendererType";
	}
}

class PhpFaces_XHTML {
	const NS = "http://www.w3.org/1999/xhtml";
	const XML_VERSION = "1.0";
	const ENCODING = "ISO-8859-1";
}

// ======================== Renderer Implementations =================================

class PhpFaces_RenderKit_Html_HtmlButtonRenderer extends PhpFaces_Render_Renderer {
	public function encodeBegin( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		$writer = $facesContext->getResponseWriter();
		
		// TODO support type=image and type=button
		
		$writer->startElementNs( null, "input", PhpFaces_XHTML::NS );
		$writer->writeAttribute( "id", $component->getId() );
		$writer->writeAttribute( "type", "submit" ); // TODO constant
		$value = $component->getValue();
		if( $value != null ) {
			$writer->writeAttribute( "value", $value );
		}
		$writer->endElement();
		
	}
	
	public function encodeEnd( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		// do nothing
	}
	public function encodeChildren( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		// do nothing
	}
	public function getRendersChildren() {
		return false;
	}	
}

class PhpFaces_RenderKit_Html_HtmlFormRenderer extends PhpFaces_Render_Renderer {
	public function encodeBegin( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		$writer = $facesContext->getResponseWriter();
		
		// TODO support type=image and type=button
		
		$writer->startElementNs( null, "form", PhpFaces_XHTML::NS );
		$writer->writeAttribute( "id", $component->getId() );
		
		// String actionURL = viewHandler.getActionURL(facesContext, viewId);
		$writer->writeAttribute( "action", "#actionUrl" );
		
	}
	
	public function encodeEnd( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		$writer = $facesContext->getResponseWriter();
		
		$writer->endElement();
	}
	public function encodeChildren( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		// do nothing
	}
	public function getRendersChildren() {
		return false;
	}	
}

class PhpFaces_RenderKit_Html_HtmlTextRenderer extends PhpFaces_Render_Renderer {
	public function encodeBegin( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {

		if( $component instanceof PhpFaces_Component_UIInput ) {
			$this->renderInput( $facesContext, $component );
		} else if( $component instanceof PhpFaces_Component_UIOutput ) {
			$this->renderOutput( $facesContext, $component );
		} else {
			//throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
		}
	}
	
	protected static function renderInput( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		$writer = $facesContext->getResponseWriter();
		
		$writer->startElementNs( null, "input", PhpFaces_XHTML::NS );
		$writer->writeAttribute( "id", $component->getId() );
		$value = $component->getValue();
		if( $value != null ) {
			$writer->writeAttribute( "value", $value );
		}
		$writer->endElement();
	}
	
	protected static function renderOutput( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		// String text = RendererUtils.getStringValue(facesContext, component);
		
		$text = PhpFaces_Render_RenderUtils::getStringValue( $facesContext, $component );
		
			$logger = LoggerManager::getLogger("PhpFaces.RenderKit.Html.HtmlTextRenderer");
		
		$logger->debug( "renderOutput: $component");
		
		$text = $component->getValue();
		/*
        if (component instanceof HtmlOutputText) {
            escape = ((HtmlOutputText)component).isEscape();
        } else {	
            escape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true);
        }
		 */
		if( $component instanceof PhpFaces_Component_Html_HtmlOutputText ) {
			$escape = $component->isEscape();
		} else {
		 	// FIXME
			$escape = true;
		}
		self::renderOutputText( $facesContext, $component, $text, $escape );
	}
	
	public static function renderOutputText( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component, $text, $escape = true ) {
		if( $text != null ) {
			
			$writer = $facesContext->getResponseWriter();
			
			$writer->startElementNs( null, "div", PhpFaces_XHTML::NS );
			$writer->writeAttribute( "id", $component->getId() );
			
			
			if( $escape ) {
				$writer->text( $text );
			} else {
				$writer->text( $text );
				// WTF?
				//$writer->writeRaw( $text );	
			}
			
			
			$writer->endElement();				
		}
	}
	
	public function encodeEnd( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		// do nothing
	}
	public function encodeChildren( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		// do nothing
	}
	public function getRendersChildren() {
		return false;
	}	
}



class PhpFaces_RenderKit_Html_UIViewRootRenderer extends PhpFaces_Render_Renderer {
	public function encodeBegin( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		$writer = $facesContext->getResponseWriter();
		
		//$writer->openUri( "php://stdout" );
		//$writer->openMemory();
		
		$writer->startDocument( PhpFaces_XHTML::XML_VERSION, PhpFaces_XHTML::ENCODING );
		$writer->startElementNs( null, "html", PhpFaces_XHTML::NS );
		$writer->startElementNs( null, "body", PhpFaces_XHTML::NS );
	}
	public function encodeEnd( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		$writer = $facesContext->getResponseWriter();
		$writer->endElement(); //body
		$writer->endElement(); //html
		$writer->endDocument();
		
		//echo $writer->outputMemory( true );
		$writer->flush();
	}
	public function encodeChildren( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $component ) {
		
	}
	public function getRendersChildren() {
		return false;
	}
}

?>